<?php


namespace App\Http\Controllers\admin\model;


use Illuminate\Support\Facades\DB;

class AdminSnModel
{
    public static function add($param){
        return DB::table('t_admin_sn')->insert($param);
    }

    public static function insertGetId($param){
        return DB::table('t_admin_sn')->insertGetId($param);
    }    

    public static function queryFirst($where,$field){
        return DB::table('t_admin_sn')->where($where)->first($field);
    }

    public static function valueGetId($where){
        return DB::table('t_admin_sn')->where($where)->value('id');
    }

    public static function edit($where,$param){
        return DB::table('t_admin_sn')->where($where)->update($param);
    }

    public static function queryCount($where=[],$param=[]){
        if(isset($param['type']) && $param['type']!=1){
            return DB::table('t_admin_sn as as')
                ->leftJoin('t_teacher_sn as ts','as.id','=','ts.admin_sn_id')
                ->leftJoin('t_parent_sn as ps','as.id','=','ps.admin_sn_id')
                ->leftJoin('t_admin as a','ps.admin_id','=','a.id')
                ->leftJoin('t_admin as ad','ps.teacher_id','=','ad.id')
                ->leftJoin('t_bind_sn as bsn','bsn.sn_value','=','as.value')
                ->where($where)
                ->where("$param[field]",'like','%'.$param["$param[field]"].'%')
                ->count('as.id');
        }
        return DB::table('t_admin_sn as as')
            ->leftJoin('t_teacher_sn as ts','as.id','=','ts.admin_sn_id')
            ->leftJoin('t_parent_sn as ps','as.id','=','ps.admin_sn_id')
            ->leftJoin('t_admin as a','ps.admin_id','=','a.id')
            ->leftJoin('t_admin as ad','ps.teacher_id','=','ad.id')
            ->leftJoin('t_bind_sn as bsn','bsn.sn_value','=','as.value')
            ->where($where)
            ->count('as.id');
    }

    public static function queryAll($where=[],$param=[],$page){
        $field = ['as.id','as.value','as.name','as.create_time','as.school','as.online','as.off_line_time','a.username as pUsername','a.mobile',
            'as.mac','ad.username as tUsername','b.name as bName','b.id as batchId','su.uid','su.grade','su.card_info','as.unbound_time','as.appstore_version','as.updatetools_version','as.info_version','as.jiaoguanyi_version','as.gankao_version','as.learning_version','as.chat_version','as.uiuios_version','as.jpush_id','bsn.status','as.type','as.type_id','g.title as type_name'];
        if(isset($param['type']) && $param['type']!=1) {
            return DB::table('t_admin_sn as as')
                ->leftJoin('t_group as g','g.id','=','as.type_id')
                ->leftJoin('t_teacher_sn as ts', 'as.id', '=', 'ts.admin_sn_id')
                ->leftJoin('t_parent_sn as ps', 'as.id', '=', 'ps.admin_sn_id')
                ->leftJoin('t_admin as a', 'ps.admin_id', '=', 'a.id')
                ->leftJoin('t_admin as ad', 'ps.teacher_id', '=', 'ad.id')
                ->leftJoin('t_batch as b','ts.batch_id','=','b.id')
                ->leftJoin('t_sn_uid as su','su.sn','=','as.value')
                ->leftJoin('t_bind_sn as bsn','bsn.sn_value','=','as.value')
                ->where($where)
                ->where("$param[field]", 'like', '%' . $param["$param[field]"] . '%')
                ->offset($page['startSize'])
                ->limit($page['pageSize'])
                ->get($field)->toArray();
        }
        return DB::table('t_admin_sn as as')
            ->leftJoin('t_group as g','g.id','=','as.type_id')
            ->leftJoin('t_teacher_sn as ts', 'as.id', '=', 'ts.admin_sn_id')
            ->leftJoin('t_parent_sn as ps', 'as.id', '=', 'ps.admin_sn_id')
            ->leftJoin('t_admin as a', 'ps.admin_id', '=', 'a.id')
            ->leftJoin('t_admin as ad', 'ps.teacher_id', '=', 'ad.id')
            ->leftJoin('t_batch as b','ts.batch_id','=','b.id')
            ->leftJoin('t_sn_uid as su','su.sn','=','as.value')
            ->leftJoin('t_bind_sn as bsn','bsn.sn_value','=','as.value')
            ->where($where)
            ->offset($page['startSize'])
            ->limit($page['pageSize'])
            ->get($field)->toArray();
    }

    public static function queryWhereFirst($where){
        return DB::table('t_admin_sn as a')
            ->leftJoin('t_teacher_sn as ts','a.id','=','ts.admin_sn_id')
            ->leftJoin('t_parent_sn as ps','a.id','=','ps.admin_sn_id')
            ->leftJoin('t_batch as b','ts.batch_id','=','b.id')
            ->where($where)
            ->first(['a.value','ps.admin_id','ps.teacher_id','b.id']);
    }

    public static function delete($where){
        return DB::table('t_admin_sn')->where($where)->delete();
    }

    public static function queryWhereCount($where=[]){
        return DB::table('t_admin_sn')->where($where)->count('id');
    }
}